<template>
  <div class="interview" v-if="questionInfo && questionInfo.detail">
    <van-nav-bar>
      <template #left>
        <!-- 点击箭头跳转到find页面(因为find页面不需要登录就可以进入) -->
        <i
          @click="$router.push('/question')"
          class="iconfont iconbtn_nav_back"
        ></i>
      </template>
      <template #title>
        <h3>
          面试题<sup>{{ questionList.length }}</sup>
        </h3>
      </template>
      <template #right>
        <span>答题卡</span>
      </template>
    </van-nav-bar>
    <div class="content">
      <div class="title">
        【{{ typeObj[questionInfo.detail.type] }}】{{
          questionInfo.detail.title
        }}
      </div>
      <div class="tag">
        <span v-for="(item, index) in questionInfo.detail.tag" :key="index">{{
          item
        }}</span>
      </div>
      <!-- 单选的时候显示 -->
      <div class="options" v-if="questionInfo.detail.type === 1">
        <!-- 给被点击的答案设置边框效果(选中的答案与选项中的答案一致)
             ringht:当正确答案与选项中的答案一致&&已经获取到答案解析
             error:已经获取到答案解析&&答案解析中的isRight是false(说明选错了)&&选中的答案与选项中的答案一致
         -->
        <div
          :class="{
            active: singleAnswer === options[index],
            right:
              questionInfo.answer &&
              questionInfo.answer.singleAnswer === options[index],
            error:
              questionInfo.answer &&
              !questionInfo.answer.isRight &&
              singleAnswer === options[index]
          }"
          class="item"
          v-for="(item, index) in questionInfo.detail.option"
          :key="index"
          @click="selAnswer(options[index])"
        >
          {{ options[index] }}.{{ item }}
        </div>
      </div>
      <!-- 多选的时候显示 -->
      <div class="options" v-else-if="questionInfo.detail.type === 2">
        <!--
            right:答案解析存在&当前选项在答案解析的多选正确答案中
            error:答案存在&&答案解析中的isRight是false&&当前的选项在自己选的多选答案中&&当前选项不在答案解析的正确答案中
         -->
        <div
          :class="{
            active: multipleAnswer.includes(options[index]),
            right:
              questionInfo.answer &&
              questionInfo.answer.multipleAnswer.includes(options[index]),
            error:
              questionInfo.answer &&
              !questionInfo.answer.isRight &&
              multipleAnswer.includes(options[index]) &&
              !questionInfo.answer.multipleAnswer.includes(options[index])
          }"
          class="item"
          v-for="(item, index) in questionInfo.detail.option"
          :key="index"
          @click="selMultiple(options[index])"
        >
          {{ options[index] }}.{{ item }}
        </div>
      </div>
      <!-- 答案解析，提交了答案之后才会有答案解析 -->
      <div class="answer" v-if="step >= 2">
        <div class="header">答案解析</div>
        <div class="true">
          正确答案：{{
            questionInfo.answer.singleAnswer ||
              questionInfo.answer.multipleAnswer.join(',')
          }}
        </div>
        <div class="otherInfo">
          <div class="diffivulty">
            难度：{{ difficultyObj[questionInfo.answer.difficulty] }}
          </div>
          <div class="submitNum">
            提交次数：{{ questionInfo.answer.submitNum }}
          </div>
          <div class="correctNum">
            正确次数：{{ questionInfo.answer.correctNum }}
          </div>
        </div>
        <div class="analysis">
          {{ questionInfo.answer.answerAnalysis }}
        </div>
      </div>
    </div>
    <div class="bottomBar">
      <div class="items">
        <div class="item">
          <i
            @click="collectQuestion"
            class="iconfont myicon"
            :class="{
              iconicon_pingfen_sel: userInfo.collectQuestions.includes(
                questionInfo.id
              ),
              iconbtn_shoucang_sel: !userInfo.collectQuestions.includes(
                questionInfo.id
              )
            }"
          ></i>
          <span>收藏</span>
        </div>
        <div class="item">
          <i class="iconfont iconbtn_shuati_fankui"></i>
          <span>反馈</span>
        </div>
      </div>
      <div class="answerNum">
        <span>{{ currentIndex + 1 }}</span
        >/{{ questionList.length }}题
      </div>
      <div class="submitBtn">
        <!-- 选中提交答案，按钮才是启用状态 -->
        <van-button
          v-if="step < 2"
          type="danger"
          class="btn"
          :disabled="step < 1"
          @click="submit"
          >提交</van-button
        >
        <!-- 试题提交后，显示下一题按钮 -->
        <van-button
          v-else-if="step === 2"
          type="info"
          class="btn"
          @click="nextOne"
          >下一题</van-button
        >
      </div>
    </div>
  </div>
</template>
<script>
// 导入网络请求方法
import {
  getQuestionsApi,
  submitApi,
  getQuesApi,
  collectQuestionApi
} from '../../../api/interview.js'
// 导入vuex的辅助函数
import { mapState, mapActions } from 'vuex'
export default {
  data () {
    return {
      // 选项前面的
      options: 'ABCDEFG',
      city: this.$route.params.city /* 面试题对应的城市 */,
      type: this.$route.params.id /* 面试题对应的岗位id */,
      questionList: [] /* 试题列表 */,
      currentIndex: 0 /* 当前显示的题目在试题列表的下标，默认页面一加载显示第0题 */,
      step: 0 /* 表示试题的状态：0 未选 1 选中答案但未提交 2 已提交 3 后面没题了 */,
      singleAnswer: '' /* 选中的单选答案 */,
      multipleAnswer: [] /* 选中的多选答案 */,
      // 题型对象
      typeObj: {
        1: '单选',
        2: '多选',
        3: '简答'
      },
      // 难度对象
      difficultyObj: {
        1: '简单',
        2: '一般',
        3: '困难'
      }
    }
  },
  computed: {
    // 使当前的试题信息成为一个计算属性，方便书写
    questionInfo () {
      // 一开始进入时this.questionList[this.currentIndex]是不存在的，为了正常渲染在后面或一个{}
      return this.questionList[this.currentIndex] || {}
    },
    ...mapState(['userInfo'])
  },
  created () {
    // 页面一加载就获取试题列表
    this.getQuestions()
  },
  methods: {
    ...mapActions(['setUserInfoAsync']),
    // 根据城市和岗位信息发送获取面试题请求
    async getQuestions () {
      const res = await getQuestionsApi({
        city: this.city,
        type: this.type
      })
      // 保存试题列表，并渲染数据
      this.questionList = res.data
    },
    // 点击选项
    selAnswer (data) {
      // 提交后选项不能被点击了答案也不能被修改，判断试题是否提交
      if (this.step < 2) {
        // 接收被点击的选项
        this.singleAnswer = data
        // 修改试题页面的状态
        this.step = 1
      }
      // 提交了点击即无效
    },
    // 提交答案
    async submit () {
      // 根据选中的答案和试题id发送提交请求
      const res = await submitApi({
        id: this.questionInfo.id,
        singleAnswer: this.singleAnswer,
        multipleAnswer: this.multipleAnswer
      })
      // 将获取的正确答案等信息保存到试题列表的对应试题信息中
      this.questionInfo.answer = res.data
      // 修改试题状体
      this.step = 2
      // 渲染答案解析，和选项状态（是否正确）
    },
    // 点击下一题
    async nextOne () {
      // 修改当前显示的题目
      this.currentIndex++
      // 修改试题的状态
      this.step = 0
      // 清空答案(单选多选都清空)
      this.singleAnswer = ''
      this.multipleAnswer = []
      // 根据当前试题的id发送请求获取题面
      const res = await getQuesApi(this.questionInfo.id)
      // 将获取的题面信息存储到试题列表中(动态添加的数据没有响应式的特点，使用$set动态添加就可以解决)
      this.$set(this.questionInfo, 'detail', res.data)
    },
    // 多选的选项被点击
    selMultiple (data) {
      // 判断是否提交，已提交不能点击选项了
      if (this.step > 1) {
        return
      }
      // 未提交
      // 判断多选答案中是否存在被点击的选项
      const num = this.multipleAnswer.findIndex(item => {
        // item =>multipleAnswer中的每一项
        return item === data
      })
      if (num === -1) {
        // 不存在,将被点击的选项放进多选答案中
        this.multipleAnswer.push(data)
      } else {
        // 存在，删除多选答案中的该选项
        this.multipleAnswer.splice(num, 1)
      }
      // 修改试题状态
      // 被点击后，multipleAnswer中有值才能开启提交按钮
      if (this.multipleAnswer.length > 0) {
        this.step = 1
      } else {
        this.step = 0
      }
    },
    // 收藏题目&取消收藏
    async collectQuestion () {
      // 根据当前题目的id发送请求
      await collectQuestionApi({ id: this.questionInfo.id })
      // 更新vuex中userInfo中的数据
      this.setUserInfoAsync()
    }
  }
}
</script>
<style lang="less" scoped>
.right {
  position: relative;
  background-color: #ddfad9;
  padding-right: 50px !important;
  &::after {
    content: '\e604';
    position: absolute;
    top: 12px;
    right: 12px;
    font-family: 'iconfont';
    font-size: 24px;
    color: #1dc779;
  }
}
.error {
  position: relative;
  background-color: #ffefea;
  padding-right: 50px !important;
  &::after {
    content: '\e605';
    position: absolute;
    top: 12px;
    right: 12px;
    font-family: 'iconfont';
    font-size: 24px;
    color: #ff4949;
  }
}
.active {
  border: 1px solid #b4b4bd !important;
}
.iconicon_pingfen_sel {
  font-size: 24px !important;
  color: #e40137;
}
.interview {
  padding-bottom: 70px;
  .iconbtn_nav_back {
    font-size: 44px;
  }
  ::v-deep .van-nav-bar__left {
    padding: 0;
  }
  .content {
    padding: 25px 15px;
    .title {
      color: #181a39;
      font-size: 16px;
      line-height: 1.5em;
    }
    .tag {
      display: flex;
      flex-wrap: wrap;
      font-size: 12px;
      color: #b4b4bd;
      margin: 10px 0 28px;
      span {
        background-color: #f7f5f4;
        padding: 3px 5px;
        border-radius: 3px;
        margin-right: 10px;
        margin-top: 10px;
      }
    }
    .options {
      padding: 0 10px;
      .item {
        margin-bottom: 16px;
        font-size: 16px;
        color: #181a39;
        border: 1px solid #f7f4f5;
        padding: 15px 10px;
        border-radius: 5px;
      }
    }
    .header {
      color: #222222;
      font-size: 18px;
      font-weight: 600;
      padding: 20px 0;
    }
    .true {
      color: #1dc779;
      font-size: 16px;
    }
    .otherInfo {
      display: flex;
      color: #545671;
      font-size: 12px;
      padding: 9px;
      margin: 20px 0;
      background-color: #f7f4f5;
      justify-content: space-between;
      border-radius: 4px;
    }
    .analysis {
      font-size: 16px;
      color: #181a39;
      line-height: 1.35em;
    }
  }
  .bottomBar {
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #fff;
    width: 100%;
    box-sizing: border-box;
    box-shadow: 0px -3px 4px 0px rgba(24, 26, 57, 0.04);
    .items {
      font-size: 12px;
      display: flex;
      color: #b4b4bd;
      .item {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .iconfont {
          font-size: 28px;
        }
        .myicon {
          margin-right: 20px;
        }
      }
    }
    .answerNum {
      color: #545671;
      font-size: 16px;
      span {
        font-size: 22px;
        color: #e40137;
      }
    }
    .submitBtn {
      .btn {
        width: 98px;
        height: 40px;
        border-radius: 7px;
      }
    }
  }
}
</style>
